﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
using HtmlAgilityPack;
using NewsCrawler.Common.Interfaces;
using NewsCrawler.Common.Models;
using PCnet;

namespace PCNet
{
    public class PCNetParser : ICrawler
    {
        private const string MainSiteUrl = "http://www.pcnet.com.tr/w/feed/";

        public List<Article> Parse()
        {
            return GenerateArticles(MainSiteUrl);
        }

        private List<Article> GenerateArticles(string siteURL)
        {
            List<Article> articles = new List<Article>();

            HttpWebRequest client = (HttpWebRequest)HttpWebRequest.Create(siteURL);
            client.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
            //string xml = new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd().Replace("content:encoded", "content");
            string xml = PCNetParserHelper.Read();
            XDocument xdoc = XDocument.Load(new StringReader(xml));
            var links = from lv1 in xdoc.Descendants("item")
                        select
                            new
                                {
                                    url = lv1.Element("link").Value,
                                    date = DateTime.Parse(lv1.Element("pubDate").Value, CultureInfo.InvariantCulture),
                                    title = lv1.Element("title").Value,
                                    desc = lv1.Element("description").Value,
                                    content = lv1.Element("content").Value
                                };
            links = links.OrderByDescending(p => p.date).Take(10);

            foreach (var item in links)
            {
                var pCnetArticle = new Article()
                                       {
                                           Url = item.url,
                                           Title = item.title,
                                           Description = item.desc,
                                           Content = item.content,
                                           Folder = "images\\PCNet",
                                           Parser = new DummyParser()
                                       };
                articles.Add(pCnetArticle);

                string filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
                File.AppendAllText(filePath, item.content);

                HtmlDocument doc = new HtmlDocument();
                doc.Load(filePath);

                foreach (HtmlNode image in doc.DocumentNode.SelectNodes("//img"))
                {
                    string websiteImagePath = image.Attributes["src"].Value;
                    string fileSystemImagePath = GetImagePath(pCnetArticle.Folder, websiteImagePath);
                    pCnetArticle.Content = pCnetArticle.Content.Replace(websiteImagePath, fileSystemImagePath);
                    pCnetArticle.Images.Add(fileSystemImagePath);
                }

                pCnetArticle.PublishDate = item.date;
                pCnetArticle.CultureCode = "tr-TR";
            }

            return articles;
        }

        private string GetImagePath(string imagePath, string websiteImagePath)
        {
            WebClient client = new WebClient();
            client.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
            string value = websiteImagePath;
            string fileName = Path.Combine(imagePath, Path.GetFileName(value));
            //client.DownloadFile(value, fileName);
            return fileName;
        }
    }
}
